layui.define(['table', 'laypage','jquery', 'element'], function(exports) {
	"use strict";
	var filePath = layui.cache.modules.fileTable
		.substr(0, layui.cache.modules.fileTable.lastIndexOf('/'));
	// 引入fileTable.css
	layui.link(filePath + '/fileTable.css');
	var MOD_NAME = 'fileTable',
		$ = layui.jquery,
		element = layui.element,
		laypage = layui.laypage;
	var _instances = {};  // 记录所有实例
	/* 默认参数 */
	var defaultOption = {
		elem: "#currentTableId",// 构建的模型
		url: "",// 数据 url 连接
		loading: true,//是否加载
		limit: 0, //每页数量默认是每行数量的双倍
		linenum: 4, //每行数量 2,3,4,6
		currentPage: 1,//当前页
		data:[],       //静态数据
		limits:[],     //页码
		page: true, //是否分页
		layout: ['count', 'prev', 'page', 'next','limit', 'skip'],//分页控件
		request: {
			pageName: 'page' //页码的参数名称，默认：page
			, limitName: 'limit' //每页数据量的参数名，默认：limit
			, id: 'id'       //主键名称，默认：id
			, parentId: 'parentId'       //父id，默认：parentId
			, filePath: 'filePath' //图片地址，默认：filePath
			, fileName: 'fileName' //图片名称，默认：fileName
			, fileRename: 'fileRename' //附件名称(UUID)，默认：fileRename
			, fileSuffix: 'fileSuffix' //附件后缀，默认：fileSuffix
			, isDir: 'isDir' //标识 0文件 1文件夹，默认：isDir
			, isImg: 'isImg' //标识 0默认 1图片，默认：isImg
		},
		response: {
			statusName: 'code' //规定数据状态的字段名称，默认：code
			, statusCode: 0 //规定成功的状态码，默认：0
			, msgName: 'msg' //规定状态信息的字段名称，默认：msg
			, countName: 'count' //规定数据总数的字段名称，默认：count
			, dataName: 'data' //规定数据列表的字段名称，默认：data
		},
		clickItem: function(data,obj){},
		// 完 成 函 数
		done: function () {

		}
	};
	var card = function(opt) {
		_instances[opt.elem.substring(1)] = this;
		this.reload(opt);
	};
	/** 参数设置 */
	card.prototype.initOptions = function (opt) {
		this.option = $.extend(true, {}, defaultOption, opt);
		if (!this.option.limit || this.option.limit == 0) {
			this.option.limit = this.option.linenum * 2;
		}
		if (!this.option.limits || this.option.limits.length == 0) {
			this.option.limits = [this.option.limit];
        }
	};
	card.prototype.init = function () {
		var option = this.option;
		var url = option.url;
		var html = "";
		html += option.loading == true ? '      <div class="ew-table-loading">' : '      <div class="ew-table-loading layui-hide">';
		html += '         <i class="layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop"></i>';
		html += '      </div>';
		$(option.elem).html(html);
		// 根 据 请 求 方 式 获 取 数 据
		html = "";
		if (!!url) {
			if (url.indexOf("?") >= 0) {
				url = url + '&v=1.0.0';
			}
			else {
				url = url + '?v=1.0.0';
			}
			if (!!option.page) {
				url = url + '&' + option.request.limitName + '=' + option.limit;
				url = url + '&' + option.request.pageName + '=' + option.currentPage;
			}
			if (!!option.where) {
				for (let key in option.where) {
					url = url + '&' + key + '=' + option.where[key];
				}
            }
			var data = getData(url);
			data = initData(data, option);
			if (data.code != option.response.statusCode) {
				option.data = [];
				option.count = 0;
            } else {
				option.data = data.data;
				option.count = option.data.length;
			}

		}
		else {
			if (!option.alldata) {
				option.alldata = option.data;
            }
			if (option.page) {
				var data = [];
				option.count = option.alldata.length;
				for (var i = (option.currentPage - 1) * option.limit; i < option.currentPage * option.limit && i<option.alldata.length; i++) {
					data.push(option.alldata[i]);
				}
				option.data = data;
			}
		}
		// 根据结果进行相应结构的创建
		if (!!option.data && option.data.length > 0) {
			html = createComponent(option.elem.substring(1), option.linenum, option.data);
			html += "<div id='cardpage'></div>";
		}
		else {
			html = "<p id='cardTabledNone'>无数据</p>";
		}
		$(option.elem).html(html);
		if (option.page) {
			// 初始化分页组件
			laypage.render({
				elem: 'cardpage'
				, count: option.count, limit: option.limit, limits: option.limits, curr: option.currentPage
				, layout: option.layout
				, jump: function (obj, first) {
					option.limit = obj.limit;
					option.currentPage = obj.curr;
					if (!first) {
						_instances[option.elem.substring(1)].reload(option);
					}
				}
			});
		}
	}
	card.prototype.reload = function (opt) {
		this.initOptions(this.option ? $.extend(true, this.option, opt) : opt);
		this.init();  // 初始化表格
    }

	function createComponent(elem,linenum,data) {
		var html = "<div class='cloud-card-component'>"
		var content = createCards(elem, linenum,data);
        var page = "";
        content = content + page;
        html += content + "</div>"
        return html;
	}
	/** 创建指定数量的卡片 */
	function createCards(elem, linenum,data) {
		var content = "<div class='layui-row layui-col-space30'>";
		for (var i = 0; i < data.length; i++) {
			content += createCard(elem, linenum,data[i],i);
        }
		content += "</div>";
		return content;
	}
	/** 创建一个卡片 */
	function createCard(elem, linenum, item, no) {
		var line = 12 / linenum;
		var card1;
		var card2;
		var card3;
			card1 = '<div id=' + item.id + ' onclick="fileTableCheckedCard(' + elem + ',this)" class="layui-col-md' + line + ' layui-col-sm' + line + ' layui-col-xs' + line + ' ew-datagrid-item" data-index="' + no+'" data-number="1"> ' +
				'<div class="project-list-item"> ' ;
		/** 判断是否是图片 */
		if (item.isImg == 1){
			card2 = '<img class="project-list-item-cover" style="width: 56px;height: 56px;" src="' +fjHttp+item.filePath + '"/> ' ;
		}else {
			card2 = '<img class="project-list-item-cover" src="../../../static/admin/img/icon/file/' +item.fileSuffix + '.png"/> ' ;
		}
			card3 = '<div class="project-list-item-body"> '+
						'<h2 lay-tips="' + item.fileName + '">' + item.fileName + '</h2> '+
						'<div class="project-list-item-text layui-text file-parentId layui-hide">' +item.parentId + '</div> ' +
						'<div class="project-list-item-text layui-text file-fileSuffix layui-hide">' +item.fileSuffix + '</div> ' +
						'<div class="project-list-item-text layui-text file-filePath layui-hide">' +item.filePath + '</div> ' +
						'<div class="project-list-item-text layui-text file-isDir layui-hide">' +item.isDir + '</div> ' +
						'<div class="project-list-item-text layui-text file-isImg layui-hide">' +item.isImg + '</div> ' +
						'<div class="project-list-item-desc layui-hide"> ' +
							'<span class="time"></span> ' +
							'<div class="ew-head-list"></div> ' +
						'</div> ' +
					'</div > ' +
				'</div > ' +
			'</div > ';
		var card = 	card1 + card2 + card3;
		return card;
	}
	/** 格式化返回参数 */
	function initData(tempData, option) {
		var data = {};
		data.code = tempData[option.response.statusName];
		data.msg = tempData[option.response.msgName];
		data.count = tempData[option.response.countName];
		var dataList = tempData[option.response.dataName];
		data.data = [];
		for (var i = 0; i < dataList.length; i++) {
			var item = {};
			item.id = dataList[i][option.request.id];
			item.parentId = dataList[i][option.request.parentId];
			item.filePath = dataList[i][option.request.filePath];
			item.fileName = dataList[i][option.request.fileName];
			item.fileRename = dataList[i][option.request.fileRename];
			item.fileSize = dataList[i][option.request.fileSize];
			item.fileSuffix = dataList[i][option.request.fileSuffix];
			item.isDir = dataList[i][option.request.isDir];
			item.isImg = dataList[i][option.request.isImg];
			data.data.push(item);
		}
		return data;
    }
	/** 同 步 请 求 获 取 数 据 */
	function getData(url) {
		$.ajaxSettings.async = false;
		var redata = null;
		$.getJSON(url, function (data) {
			redata = data;
		}).fail(function () {
			redata = null;
		});
		return redata;
	}
	//卡片点击事件
	window.fileTableCheckedCard = function (elem,obj) {
		$(obj).addClass('layui-table-click').siblings().removeClass('layui-table-click');
		var item = {};
		item.id = obj.id;
		item.fileName = $(obj).find('h2')[0].innerHTML;
		item.parentId = $(obj).find('.file-parentId')[0].innerHTML;
		item.fileSuffix = $(obj).find('.file-fileSuffix')[0].innerHTML;
		item.filePath = $(obj).find('.file-filePath')[0].innerHTML;
		item.isDir = $(obj).find('.file-isDir')[0].innerHTML;
		item.isImg = $(obj).find('.file-isImg')[0].innerHTML;
		_instances[elem.id].option.checkedItem = item;
		_instances[elem.id].option.clickItem(item,obj);
	}
	/** 对外提供的方法 */
	var tt = {
		/* 渲染 */
		render: function (options) {
			return new card(options);
		},
		/* 重载 */
		reload: function (id, opt) {
			_instances[id].option.checkedItem = null;
			_instances[id].reload(opt);
		},
		/* 获取选中数据 */
		getChecked: function (id) {
			var option = _instances[id].option;
			var data = option.checkedItem;
			var item = {};
            if (!data) {
				return null;
            }
			item[option.request.id] = data.id;
			item[option.request.parentId] = data.parentId;
			item[option.request.filePath] = data.filePath;
			item[option.request.fileName] = data.fileName;
			item[option.request.fileRename] = data.fileRename;
			item[option.request.fileSize] = data.fileSize;
			item[option.request.fileSuffix] = data.fileSuffix;
			item[option.request.isDir] = data.isDir;
			item[option.request.isImg] = data.isImg;
			return item;
		},
		/* 获取表格数据 */
		getAllData: function (id) {
			var option = _instances[id].option;
			var data = [];
			for (var i = 0; i < option.data.length; i++) {
				var item = {};
				item[option.request.id] = option.data[i].id;
				item[option.request.parentId] = option.data[i].parentId;
				item[option.request.filePath] = option.data[i].filePath;
				item[option.request.fileName] = option.data[i].fileName;
				item[option.request.fileRename] = option.data[i].fileRename;
				item[option.request.fileSize] = option.data[i].fileSize;
				item[option.request.fileSuffix] = option.data[i].fileSuffix;
				item[option.request.isDir] = option.data[i].isDir;
				item[option.request.isImg] = option.data[i].isImg;
				data.push(item);
            }
			return data;
		},
	}
	exports(MOD_NAME, tt);
})
